//
//  ConNewHomeVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/6.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConNewHomeVc.h"
#import "TGSementBarVC.h"
#import "ConNewClubVc.h"
#import "ConYueViewController.h"
#import "ConPickViewController.h"
#import "MBProgressHUD+MJ.h"
#import "ConNewMapVc.h"
#import "SDCycleScrollView.h"
#import <MJRefresh.h>
#import "QuickButtonCell.h"
#import "PaomaViewCell.h"
#import "HomeHeaderImgCell.h"
#import "CommendAcCell.h"
#import "HomeCommendClubCell.h"
#import "ConNewAllClubVc.h"
#import "ConInformationController.h"
#import "HuisuoJidiVc.h"
#import "NewHomeNetwork.h"
#import "HomeCommendModel.h"
#import "ConYueViewController.h"
#import "ConNewClubDetailVc.h"
#import "ConeBaseViewController.h"
#import "NewMapModel.h"
#import "ConNearAcInfoViewController.h"
#import "SZLAccountHelper.h"
#import "ConReportViewController.h"
#import "NSDate+Extension.h"
#import "ConYueVc.h"
#import <MJRefresh.h>
#import "SZLTabBarViewController.h"
#import "PersonNetWork.h"
#import "PersonInfo.h"
#import "ClubNetwork.h"

@interface ConNewHomeVc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) TGSementBarVC *segmentBarVC;
@property (nonatomic,weak) UIView *acView;
@property (nonatomic,weak) SDCycleScrollView *roolView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *soureces;
@property (nonatomic,strong) NSArray *rImgs;

@end

@implementation ConNewHomeVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    [self setNaviLine];
//    [self setSegmentController];
    self.navigationItem.title = @"玩转地球";
    self.rImgs = @[@"banner1",@"banner2",@"banner3"];
    [self setUpTableView];
    [self setRoolView];
    [self getHomeCommend];
}
- (void)setUpTableView
{
    UIButton *mapBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [mapBtn setImage:[UIImage imageNamed:@"地图"] forState:UIControlStateNormal];
    [mapBtn addTarget:self action:@selector(mapClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:mapBtn];
    [self.view addSubview:self.tableView];
}
//新轮播图
- (void)setRoolView
{
    SDCycleScrollView *roolView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenW, ScreenW*155/375) delegate:nil placeholderImage:[UIImage imageNamed:@"default_large_goods_image"]];
    roolView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    roolView.autoScrollTimeInterval = 3.0f;
    roolView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.tableView.tableHeaderView = roolView;
    self.roolView = roolView;
    roolView.localizationImageNamesGroup = self.rImgs;
    roolView.clickItemOperationBlock = ^(NSInteger count){//轮播图跳转
        
    };
}
#pragma ----------------tableView-----------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2+self.soureces.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return 1;
    }
    if (self.soureces.count>0) {
        HomeCommendModel *model = self.soureces[section-2];
        return model.detailList.count+1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 6;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 35;
    }else if (indexPath.section == 1) {
        return 90;
    }
    if (indexPath.row == 0) {
        return 12+ScreenW*140/750.0+25;
    }
    return 44+ScreenW*130/750;
//    if (indexPath.section == 2) {
//        if (indexPath.row == 0) {
//            return 12+ScreenW*140/750.0+25;
//        }
//        return 44+(ScreenW*210/750)*65/105;
//    }else if (indexPath.section == 3) {
//        if (indexPath.row == 0) {
//            return 12+ScreenW*140/750.0+25;
//        }
//        return 44+ScreenW*130/750;
//    }
//    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PaomaViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PaomaViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1) {
        QuickButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([QuickButtonCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.btnClick = ^(NSInteger type) {
            switch (type) {
                case 0:
                {
                    ConNewAllClubVc *vc = [[ConNewAllClubVc alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 1:
                {
                    ConInformationController *vc = [[ConInformationController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:
                {
                    ConPickViewController *vc = [[ConPickViewController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 3:
                {
                    HuisuoJidiVc *vc = [[HuisuoJidiVc alloc]init];
                    vc.type = 3;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 4:
                {
                    HuisuoJidiVc *vc = [[HuisuoJidiVc alloc]init];
                    vc.type = 2;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
        };
        return cell;
    }else if (indexPath.section >= 2) {
        HomeCommendModel *model = self.soureces[indexPath.section-2];
        if (indexPath.row == 0) {
            HomeHeaderImgCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeHeaderImgCell class])];
            cell.separatorInset = UIEdgeInsetsMake(0, ScreenW, 0, 0);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = model;
            cell.jumpBlock = ^(NSString *type, HomeCommendModel *model) {
                NSString *typeFirst = [type substringToIndex:2];
                NSString *typeSecond = [type substringFromIndex:2];
                if ([typeFirst isEqualToString:@"01"]) {//活动
                    ConYueViewController *vc = [[ConYueViewController alloc]init];
                    vc.type = [typeSecond integerValue];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if ([typeFirst isEqualToString:@"02"]) {//机构
                    ConNewAllClubVc *vc = [[ConNewAllClubVc alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            };
            return cell;
        }
        NSString *typeFirst = [model.type substringToIndex:2];
//        NSString *typeSecond = [model.type substringFromIndex:2];
        if ([typeFirst isEqualToString:@"01"]) {// 1活动 2机构
            CommendAcCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommendAcCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.separatorInset = UIEdgeInsetsMake(0, 15+(ScreenW*210/750), 0, 0);
            cell.model = model.detailList[indexPath.row-1];
            cell.reportClick = ^(HomeCommendDetailModel *model) {
                [self reportClick:model];
            };
            return cell;
        }
        HomeCommendClubCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeCommendClubCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.separatorInset = UIEdgeInsetsMake(0, 15+(ScreenW*130/750), 0, 0);
        cell.model = model.detailList[indexPath.row-1];
        return cell;
    } 
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < 2) return;
    if (indexPath.row == 0) return;
    if (self.soureces.count > 0) {
        HomeCommendModel *model = self.soureces[indexPath.section-2];
        HomeCommendDetailModel *dModel = model.detailList[indexPath.row-1];
        NSString *typeFirst = [model.type substringToIndex:2];
        if ([typeFirst isEqualToString:@"01"]) {
            ConNearAcInfoViewController *infoVc = [[ConNearAcInfoViewController alloc]init];
            infoVc.ID = [dModel.relevanceId integerValue];
            [self.navigationController pushViewController:infoVc animated:YES];
        }else if ([typeFirst isEqualToString:@"02"]) {
            ConNewClubDetailVc *detailVc = [[ConNewClubDetailVc alloc]init];
            detailVc.ID = [dModel.relevanceId integerValue];
            [self.navigationController pushViewController:detailVc animated:YES];
        }
    }
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight-49)];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PaomaViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([PaomaViewCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([QuickButtonCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([QuickButtonCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeHeaderImgCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeHeaderImgCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CommendAcCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CommendAcCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCommendClubCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeCommendClubCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.soureces removeAllObjects];
            [self getHomeCommend];
        }];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 15)];
    }
    return _tableView;
}
//获取推荐内容
- (void)getHomeCommend
{
    [NewHomeNetwork getHomeCommendWithParame:nil response:^(BOOL success, NSString *msg, NSArray *sources) {
        [self.tableView.mj_header endRefreshing];
        if (success) {
            [self.soureces addObjectsFromArray:sources];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
//活动报名
- (void)reportClick:(HomeCommendDetailModel *)model
{
    if ([SZLAccountHelper shouldLogin]) return;
//    if ([[SZLAccountHelper shareInstance].mobilePhone isEqualToString:self.detailModel.mobilePhone]) {
//        [MBProgressHUD showError:@"不能报名自己的活动" toView:self.view];
//        return;
//    }
    NSDate *holdDate = [NSDate stringToDate:[NSString stringWithFormat:@"%lld",[model.startDate longLongValue]/1000]];
    BOOL isPast = [holdDate isInPast];
    if (isPast) {
        [MBProgressHUD showError:@"活动已举办完成" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"activityId"] = model.relevanceId;
    parame[@"registrationName"] = [SZLAccountHelper shareInstance].name;
    parame[@"registrationPhone"] = [SZLAccountHelper shareInstance].mobilePhone;
    [self showClearBgWaiter];
    [ClubNetwork nearActivityReportWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"报名成功"];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (NSMutableArray *)soureces
{
    if (!_soureces) {
        _soureces = [NSMutableArray array];
    }
    return _soureces;
}









#pragma -----------------------老版代码---------------------------------
//设置naviBar的线
- (void)setNaviLine
{
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 1)];
    lineView.backgroundColor = RGBColorOne(200);
    lineView.layer.shadowColor = [UIColor blackColor].CGColor;
    lineView.layer.shadowOpacity = 0.3f;
    lineView.layer.shadowRadius = 1.f;
    lineView.layer.shadowOffset = CGSizeMake(0.5,0.5);
    [self.view addSubview:lineView];
}
//展示活动
- (void)showActivity
{
    UIView *acView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    acView.backgroundColor = RGBA(0, 0, 0, 0.6);
    UITapGestureRecognizer *acViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(acViewClick)];
    [acView addGestureRecognizer:acViewTap];
    self.acView = acView;
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, (ScreenH-ScreenW)/2.0, ScreenW, ScreenW)];
    img.image = GetImage(@"icon_promotion");
    img.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(acClick)];
    [img addGestureRecognizer:tap];
    [acView addSubview:img];
    UIWindow *key = [UIApplication sharedApplication].keyWindow;
    [key addSubview:acView];
}
//活动跳转
- (void)acClick
{
    
}
//点击空白处
- (void)acViewClick
{
    [self.acView removeFromSuperview];
}

- (void)mapClick
{
    ConNewMapVc *vc = [[ConNewMapVc alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)setSegmentController
{
    UIButton *mapBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [mapBtn setImage:[UIImage imageNamed:@"mapImg"] forState:UIControlStateNormal];
    [mapBtn addTarget:self action:@selector(mapClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:mapBtn];
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 235, 42)];
    self.segmentBarVC.segmentBar.frame = CGRectMake(0, 8, 235, 32);
    self.navigationItem.titleView = titleView;
    titleView.clipsToBounds = YES;
    [titleView addSubview:self.segmentBarVC.segmentBar];
    //设置segmentBarVC大小
    self.segmentBarVC.view.frame = CGRectMake(0, 0, ScreenW, ScreenH-49-SafeAreaBottomHeight);
    [self.view addSubview:self.segmentBarVC.view];
    [self addChildViewController:self.segmentBarVC];
    
    NSArray *items = @[@"俱乐部", @"约么", @"人脉圈"];
    NSMutableArray* childVCs = [NSMutableArray array];
    [childVCs addObject:[[ConNewClubVc alloc]init]];
    [childVCs addObject:[[ConYueViewController alloc]init]];
    [childVCs addObject:[[ConPickViewController alloc]init]];
    [self.segmentBarVC setupWithItems:items childVCs:childVCs];
    [self.segmentBarVC.segmentBar updateViewWithConfig:^(TGSegmentConfig *config) {
        config.selectedColor([UIColor blackColor])
        .normalColor([UIColor grayColor])
        .selectedFont(Font(18))//选中字体大于其他正常标签的字体的情况下，根据情况稍微调大margin（默认8），以免选中的字体变大后挡住其他正常标签的内容
        .normalFont(Font(18))
        .indicateExtraW(0)
        .indicateH(2)
        .indicateColor(RGBColor(234, 111, 90))//指示器颜色
        .showMore(NO)//是否显示更多面板
        .circleScroll(NO)//是否循环滚动，第0个再向前，那么到最后一个;最后一个向后，那么到第0个
        .moreCellBGColor([[UIColor grayColor] colorWithAlphaComponent:0.3])
        .moreBGColor([UIColor clearColor])
        .moreCellFont([UIFont systemFontOfSize:13])
        .moreCellTextColor(NavTinColor)
        .moreCellMinH(30)
        .showMoreBtnlineView(NO)
        .moreBtnlineViewColor([UIColor lightTextColor])
        .moreBtnTitleFont([UIFont systemFontOfSize:13])
        .moreBtnTitleColor([UIColor lightTextColor])
        .margin(18)
        .barBGColor([UIColor whiteColor])
        ;
    }];
}
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
}
#pragma mark --------------------new------------------------
- (TGSementBarVC *)segmentBarVC
{
    if (!_segmentBarVC) {
        _segmentBarVC = [[TGSementBarVC alloc] init];
    }
    return _segmentBarVC;
}



@end
